package com.mfoyou.agent.web.service.thirdPart;

import com.alibaba.fastjson.JSON;
import com.mfoyou.agent.web.utils.redis.RedisCacheManger;
import com.tencent.xinge.PushData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 信鸽推送服务
 *
 * @author hzhang7
 */
@Service
public class XGPushSerivce {

    private static Logger logger = LoggerFactory.getLogger(XGPushSerivce.class);

    @Autowired
    RedisCacheManger redisCacheManger;

    /**
     * 推送给接单员有新的订单
     */
    private static String NEWORDERPUSH = "xp:Line1";

    /**
     * 推送给代理商，检查2分钟后是否有人接单，没接单就指派
     */
    private static String NEWORDERAGENTCHECK = "xp:Line6";

    /**
     * 推送给所有人
     */
    private static String NEWORDERCHECK = "xp:Line2";

    /**
     * 所有用户都能看见时间
     */
    private static String ORDERALLSEECHECK = "xp:Line3";

    /**
     * 10分钟检查一下订单是否被接 如果没有就通知一下
     */
    private static String ORDERCHECKORDER = "xp:Line4";

    /**
     * 催单
     */
    private static String REMINDERRUNNER = "xp:Line5";

    /**
     * 实时通知
     */
    private static String NOTICEINFO = "xp:Line7";

    private static String EXTRACT = "xp:Line8";

    private static String TRANSFER_NOTICE = "xp:Line11";

    private static String TRANSFER_CANCLE_NOTICE = "xp:Line12";

    /**
     * 推送额外提醒
     *
     * @param userId    跑腿小哥id
     * @param orderId   订单id
     * @param userType  用户类型
     * @param title     标题
     * @param content   内容
     */
    public void pushExtractNotify(Integer userId, Integer orderId, Integer userType, String title, String content) {

        PushData pushData = pushNoticeByUserId(userId, orderId, userType, title, content);
        redisCacheManger.pushRList(EXTRACT, JSON.toJSONString(pushData));
    }

    /**
     * 团队实时指派订单 xp:Line7
     *
     * @param userId    跑腿小哥id
     * @param orderId   订单id
     * @param userType  用户类型
     * @param title     标题
     * @param content   内容
     */
    public void pushNoticeUser(Integer userId, Integer orderId, Integer userType, String title, String content) {

        PushData pushData = pushNoticeByUserId(userId, orderId, userType, title, content);
        redisCacheManger.pushRList(NOTICEINFO, JSON.toJSONString(pushData));
    }

    /**
     * 根据指定userId推送提醒
     *
     * @param userId
     * @param orderId
     * @param userType
     * @param title
     * @param content
     * @return
     */
    private PushData pushNoticeByUserId(Integer userId, Integer orderId, Integer userType, String title, String content) {
        List<String> userIds = new ArrayList<>();
        if (userId != null) {
            if (userId < 10)
                userIds.add("0" + userId);
            else
                userIds.add("" + userId);
        }
        PushData pushData = new PushData();
        pushData.setUserIds(userIds);
        pushData.setOrderId(orderId);
        pushData.setUserType(userType);
        pushData.setTitlePrefix(title);
        pushData.setContent(content);
        return pushData;
    }

    /**
     * 查看订单状态
     *
     * @param orderId   订单id
     * @param effectTime    有效时间
     */
    public void checkOrderState(Integer orderId, Long effectTime) {

        PushData pushData = new PushData();
        pushData.setOrderId(orderId);
        pushData.setEffectTime(effectTime);
        redisCacheManger.pushRList(ORDERCHECKORDER, JSON.toJSONString(pushData));

    }

    /**
     * 推送给代理商，检查2分钟后是否有人接单，没接单就指派 xp:Line6
     *
     * @param agentId
     * @param orderId
     * @param userType
     * @param type
     * @param effectTime
     */
    public void pushNoticeNewOrderAgentCheck(Integer agentId, Integer orderId, Integer userType, Integer type, Long effectTime) {

        List<String> userIds = new ArrayList<>();
        if (agentId < 10)
            userIds.add("0" + agentId);
        else
            userIds.add("" + agentId);
        PushData pushData = new PushData();
        pushData.setUserIds(userIds);
        pushData.setOrderId(orderId);
        pushData.setType(type);
        pushData.setEffectTime(effectTime);
        pushData.setUserType(userType);
        redisCacheManger.pushRList(NEWORDERAGENTCHECK, JSON.toJSONString(pushData));
    }

    /**
     * 推送给所有人 xp:Line2
     *
     * @param userIds   跑腿小哥id以及代理商id 集合
     * @param orderId   订单id
     * @param userType  用户类型
     * @param type      订单状态
     * @param effectTime
     */
    public void pushNoticeNewOrderCheck(List<String> userIds, Integer orderId, Integer userType, Integer type, Long effectTime) {

        PushData pushData = new PushData();
        pushData.setUserIds(userIds);
        pushData.setOrderId(orderId);
        pushData.setType(type);
        pushData.setEffectTime(effectTime);
        pushData.setUserType(userType);
        redisCacheManger.pushRList(NEWORDERCHECK, JSON.toJSONString(pushData));

    }

    /**
     * 推送新订单提醒给跑腿小哥 xp:Line1
     *
     * @param userIds   跑腿小哥id
     * @param orderId   订单id
     * @param userType  用户类型 1-企业用户; 2-个人用户
     * @param type
     * @param effectTime 有效时间
     */
    public void pushNoticeNewOrderToRuner(List<String> userIds, Integer orderId, Integer userType, Integer type, Long effectTime) {

        PushData pushData = new PushData();
        pushData.setUserIds(userIds);
        pushData.setOrderId(orderId);
        pushData.setType(type);
        pushData.setEffectTime(effectTime);
        pushData.setUserType(userType);
        if (userType != null && userType == 2) {
            pushData.setTitlePrefix("未分配的新订单");
        } else {
            pushData.setTitlePrefix("新订单提醒");
        }
        // 将订单存入 redis 缓存队列, TaskController.runxpLine1 方法消费订单
        logger.info(String.format("将订单存入redis缓存 消费类型=[%s] 缓存对象 pushData=[%s]", NEWORDERPUSH, JSON.toJSONString(pushData)));
        redisCacheManger.pushRList(NEWORDERPUSH, JSON.toJSONString(pushData));

    }

    /**
     * 推送催单提醒 xp:Line5
     *
     * @param userIds   跑腿小哥id
     * @param orderId   订单id
     * @param userType  用户类型 1-企业用户; 2-个人用户
     * @param type      订单状态 101-已接单;102-开始服务;103-完成服务
     * @param effectTime 有效时间
     */
    public void pushNoticeReminderToRunner(List<String> userIds, Integer orderId, Integer userType, Integer type, Long effectTime) {

        PushData pushData = new PushData();
        pushData.setUserIds(userIds);
        pushData.setOrderId(orderId);
        pushData.setType(type);
        pushData.setEffectTime(effectTime);
        pushData.setUserType(userType);
        pushData.setTitlePrefix("催单提醒");
        redisCacheManger.pushRList(REMINDERRUNNER, JSON.toJSONString(pushData));
    }

    /**
     * 推送订单提醒给指定跑腿小哥 xp:Line1
     *
     * @param userId    用户id
     * @param orderId   订单id
     * @param userType  用户类型
     * @param type      订单状态 101-已接单;102-开始服务;103-完成服务
     * @param prefix    前缀
     */
    public void pushOrderNoticeUser(Integer userId, Integer orderId, Integer userType, Integer type, String prefix) {

        List<String> userIds = new ArrayList<>();
        if (userId != null) {
            if (userId < 10)
                userIds.add("0" + userId);
            else
                userIds.add("" + userId);
        }
        PushData pushData = new PushData();
        pushData.setUserIds(userIds);
        pushData.setOrderId(orderId);
        pushData.setType(type);
        pushData.setEffectTime(System.currentTimeMillis() - 100000);
        pushData.setUserType(userType);
        pushData.setTitlePrefix(prefix);
        redisCacheManger.pushRList(NEWORDERPUSH, JSON.toJSONString(pushData));
    }

    /**
     * 已转派订单提醒
     *
     * @param userId
     * @param orderId
     * @param userType
     * @param type
     * @param title
     * @param content
     */
    public void transferPushNoticeToUser(Integer userId, Integer orderId, Integer userType, Integer type, String title, String content) {

        PushData pushData = transferNotice(userId, orderId, userType, type, title, content);
        redisCacheManger.pushRList(TRANSFER_NOTICE, JSON.toJSONString(pushData));
    }

    /**
     * 转派订单取消提醒
     *
     * @param userId
     * @param orderId
     * @param userType
     * @param type
     * @param title
     * @param content
     */
    public void transferCanclePushNoticeToUser(Integer userId, Integer orderId, Integer userType, Integer type, String title, String content) {

        PushData pushData = transferNotice(userId, orderId, userType, type, title, content);
        redisCacheManger.pushRList(TRANSFER_CANCLE_NOTICE, JSON.toJSONString(pushData));
    }

    /**
     * 转派相关提醒
     *
     * @param userId
     * @param orderId
     * @param userType
     * @param type
     * @param title
     * @param content
     * @return
     */
    private PushData transferNotice(Integer userId, Integer orderId, Integer userType, Integer type, String title, String content) {
        List<String> userIds = new ArrayList<>();
        if (userId != null) {
            if (userId < 10)
                userIds.add("0" + userId);
            else
                userIds.add("" + userId);
        }
        PushData pushData = new PushData();
        pushData.setUserIds(userIds);
        pushData.setOrderId(orderId);
        pushData.setType(type);
        pushData.setEffectTime(System.currentTimeMillis() - 100000);
        pushData.setUserType(userType);
        pushData.setTitlePrefix(title);
        pushData.setContent(content);
        return pushData;
    }
}
